package com.heima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.SkuClassMapper;
import com.heima.pojo.SkuClass;
import com.heima.pojo.dto.SkuClassDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.SkuClassVo;
import com.heima.service.SkuClassService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class SkuClassServiceImpl extends ServiceImpl<SkuClassMapper, SkuClass> implements SkuClassService {
    @Autowired
    private SkuClassMapper skuClassMapper;

    @Autowired
    private SkuClassService skuClassService;

    /**
     * 新增分类
     *
     * @param skuClassDto
     */
    @Override
    public void add(SkuClassDto skuClassDto) {
        SkuClass skuClass = new SkuClass();
        BeanUtils.copyProperties(skuClassDto, skuClass);
        skuClassMapper.insert(skuClass);
    }

    /**
     * 分页查询分类
     *
     * @param pageIndex
     * @param pageSize
     * @param className
     * @return
     */
    @Override
    public PageResponse search(Integer pageIndex, Integer pageSize, String className) {
        if (ObjectUtils.isEmpty(pageIndex)){
            pageIndex = 1;
        }
        if (ObjectUtils.isEmpty(pageSize)){
            pageSize = 10;
        }

        PageHelper.startPage(pageIndex,pageSize);
        Page<SkuClassVo> page = skuClassMapper.search(className);

        PageResponse pageResponse = new PageResponse();
        pageResponse.setPageIndex(pageIndex);
        pageResponse.setPageSize(pageSize);
        pageResponse.setTotalCount((int) page.getTotal());
        pageResponse.setCurrentPageRecords(page.getResult());

        return pageResponse;
    }

    @Override
    public SkuClassVo searchById(String classId) {
        SkuClassVo skuClassVo = skuClassService.searchById(classId);
        return skuClassVo;
    }

    /**
     * 修改分类
     *
     * @param classId
     */
    @Override
    public void updateByClassId(String classId, SkuClassDto skuClassDto) {
        SkuClass skuClass = new SkuClass();
        BeanUtils.copyProperties(skuClassDto, skuClass);
        skuClass.setClassId(Integer.valueOf(classId));
        skuClassMapper.updateById(skuClass);
    }

    /**
     * 根据id删除分类
     *
     * @param classId
     */
    @Override
    public void deleteByClassId(String classId) {
        skuClassMapper.deleteById(classId);
    }
}
